<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Archives;

use Think\Exception;
use Think\Log;

class ArchivesVision extends \Service\Common\BaseModel
{
    protected $name = 'eyes_archives_vision';


    //列表
    public function visionList($uid, $page_num, $page_size, $aid)
    {
        $where = [
            'aid' => $aid,
            'userid' => $uid,
        ];
        $list = (array)$this->where($where)->order('id desc')->page($page_num, $page_size)->select();
        $count = $this->where($where)->count();
        return [
            $this->format($list),
            $page_count = $count,
            $page_total = ceil($count / $page_size),
        ];
    }

    public function visionAdd($uid, $param){
        if($param['relation'] == 1){
            $flag = M('eyes_archives')->where(['relation' =>1,'userid' => $uid])->find();
            if($flag) throw new Exception('自己的档案已添加');
        }
        $add = [
            'userid' => $uid,
            'aid' => $param['aid'],
            'sex' => $param['sex'],
            'sph_left' => $param['sph_left'],
            'sph_right' => $param['sph_right'],
            'pd' => $param['pd'],
            'cyl_left' => $param['cyl_left'],
            'cyl_right' => $param['cyl_right'],
            'birthday' => $param['birthday'],
            'axi_left' => $param['axi_left'],
            'axi_right' => $param['axi_right'],
            'optometry_time' => $param['optometry_time'],
        ];
        $flag = $this->add($add);
        if($flag){
            $flag1 = M('eyes_task_log')->where(['taskid' => 7, 'userid' => $uid, 'record_date' => date('Y-m-d')])->find();
            if(!$flag1) OE('user')->addEyesMoney($uid, 7);
            return true;
        }else{
            throw new Exception('添加失败');
        }
    }

    public function visionModify($uid, $param){
        $data = M('eyes_archives_vision')->where(['id' =>$param['aid'],'userid' => $uid])->find();
        if(!$data) throw new Exception('验光记录未找到');
        $save = [
            'sex' => $param['sex'],
            'sph_left' => $param['sph_left'],
            'sph_right' => $param['sph_right'],
            'pd' => $param['pd'],
            'cyl_left' => $param['cyl_left'],
            'cyl_right' => $param['cyl_right'],
            'birthday' => $param['birthday'],
            'axi_left' => $param['axi_left'],
            'axi_right' => $param['axi_right'],
            'optometry_time' => $param['optometry_time'],
        ];
        $flag = $this->where(['id' =>$param['aid'],'userid' => $uid])->save($save);
        if($flag){
            return true;
        }else{
            throw new Exception('修改失败');
        }
    }

    public function visionDetail($uid, $param){
        $flag = $this->where(['id' =>$param['id'],'userid' => $uid])->find();
        if($flag){
            return $this->format($flag);
        }else{
            throw new Exception('查询失败');
        }
    }

    public function visionDel($uid, $param){
        $flag = $this->where(['id' =>$param['id'],'userid' => $uid])->find();
        if($flag){
            $this->where(['id' =>$param['id'],'userid' => $uid])->delete();
        }else{
            throw new Exception('已删除或者记录不存在');
        }
    }


    //格式化
    public function format($list)
    {
        if (!$list) {
            return $list;
        }

        $is_one = count($list) == count($list, 1);
        $list = $is_one ? [$list] : $list;

        foreach ($list as &$row) {
//            $row['relation_label'] = $this->relate[$row['relation']];
        }
        return $is_one ? $list[0] : $list;
    }



}